AJS.toInit(function ($) {
	"use strict";
	var dlg_update = null, dlg_update_memo = null,

	onCreateNoteByAjaxCompleted = function (data) {
		// if action success then reload page, else show the error message
		if (data.success === true) {
			dlg_create.hide();
			// reload page
			reloadPage();
		} else {
			// show error message
			AJS.$('.pop-up-disable').remove();
			AJS.$('#note-create-form #noteOperationErrorMessage').html(data.response.errorMessages[0]);
			AJS.$('#note-create-form .error-row').show();
		}
	},

	// This function process the submit create form event 
	onSubmitCreateForm = function () {
		AJS.$('.dialog-components').append('<div class="aui-blanket pop-up-disable"></div>');
		var request = {
			url: AJS.contextPath() + "/CreateNote.jspa",
			data: {},
			dataType: "json",
			type: "GET",
			success: onCreateNoteByAjaxCompleted
		};
		// set request data
		request.data['content'] = fixedEncodeURIComponent(AJS.$('#note-create-form textarea[name="content"]').val());
		request.data['priority'] = AJS.$('#note-create-form input[name="priority"]:checked').val();
		request.data['userName'] = AJS.$('#note-create-form input[name="userName"]').val();
		request.data['issueKey'] = AJS.$('#note-create-form input[name="issueKey"]').val();
		request.data['atl_token'] = AJS.$('#note-create-form input[name="atl_token"]').val();
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	},

	initDialogCreate = function () { 
		dlg_create = new AJS.Dialog({
			width : 600,
			height : 300,
			id : "note-create-dialog",
			closeOnOutsideClick : false
		});
		dlg_create.addHeader(AJS.I18n.getText("create.note.name"));
		dlg_create.addPanel(AJS.I18n.getText("panel.name"), AJS.renderTemplate("note-dialog-create"));
		dlg_create.addSubmit(AJS.I18n.getText("create.name"), onSubmitCreateForm);
		dlg_create.addSubmit(AJS.I18n.getText("reset.name"), resetCreateFormData);
		dlg_create.addLink(AJS.I18n.getText("cancel.name"), function (dialog) {
		    dialog.hide();
		});
	},
	
	resetCreateFormData = function () {
		var prefix = '#note-create-form ';
		AJS.$(prefix + 'input[name="priority"]:first').attr('checked', 'checked');
		AJS.$(prefix + 'input[name="userName"]').val('');
		AJS.$(prefix + 'input[name="issueKey"]').val('');
		AJS.$(prefix + 'textarea[name="content"]').val('');
		AJS.$(prefix + '#noteOperationErrorMessage').html('');
		AJS.$(prefix + '#remain').html('');
		AJS.$('.pop-up-disable').remove();
	},
	
	showCreateDialog = function () {
		if (!dlg_create) {
			AJS.$('#note-create-form').remove();
			initDialogCreate();
		}
		resetCreateFormData();
		dlg_create.show();
	},
	
	onUpdateNoteByAjaxCompleted = function (data) {
		// if action success then reload page, else show the error message
		if (data.success === true) {
			dlg_update.hide();
			// reload page if exists a input with name "reloadPageOnActionSuccess"
			if (AJS.$('input[name="reloadPageOnActionSuccess"]').length > 0) {
				reloadPage();
			} else {
				reloadLatestNote();
				reloadNoteIssueTabPanel();
			}
		} else {
			// show error message
			AJS.$('.pop-up-disable').remove();
			AJS.$('#note-update-form #noteOperationErrorMessage').html(data.response.errorMessages[0]);
			AJS.$('#note-update-form .error-row').show();
		}
	},
	
	// This function process the submit create form event 
	onSubmitUpdateForm = function () {
		AJS.$('.dialog-components').append('<div class="aui-blanket pop-up-disable"></div>');
		var request = {
			url: AJS.contextPath() + "/UpdateNote.jspa",
			data: {},
			dataType: "json",
			type: "GET",
			success: onUpdateNoteByAjaxCompleted
		};
		// set request data
		request.data['content'] = fixedEncodeURIComponent(AJS.$('#note-update-form textarea[name="content"]').val());
		request.data['id'] = AJS.$('#note-update-form input[name="id"]').val();
		request.data['priority'] = AJS.$('#note-update-form input[name="priority"]:checked').val();
		request.data['userName'] = AJS.$('#note-update-form input[name="userName"]').val();
		request.data['issueKey'] = AJS.$('#note-update-form input[name="issueKey"]').val();
		request.data['atl_token'] = AJS.$('#note-update-form input[name="atl_token"]').val();
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	},

	// This function process the submit create form event 
	onSubmitDeleteNote = function () {
		AJS.$('.dialog-components').append('<div class="aui-blanket pop-up-disable"></div>');
		var request = {
			url: AJS.contextPath() + "/DeleteNote.jspa",
			data: {},
			dataType: "json",
			type: "GET",
			success: onUpdateNoteByAjaxCompleted
		};
		// set request data
		request.data['id'] = AJS.$('#note-update-form input[name="id"]').val();
		request.data['atl_token'] = AJS.$('#note-update-form input[name="atl_token"]').val();
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	},

	initDialogUpdate = function () { 
		dlg_update = new AJS.Dialog({
			width : 600,
			height : 310,
			id : "note-update-dialog",
			closeOnOutsideClick : false
		});
		dlg_update.addHeader(AJS.I18n.getText("update.note.name"));
		dlg_update.addPanel(AJS.I18n.getText("panel.name"), AJS.renderTemplate("note-dialog-update"));
		dlg_update.addSubmit(AJS.I18n.getText("update.name"), onSubmitUpdateForm);
		dlg_update.addLink(AJS.I18n.getText("delete.name"), onSubmitDeleteNote);
		dlg_update.addLink(AJS.I18n.getText("cancel.name"), function (dialog) {
			dialog.hide();
		});
	},
	
	resetUpdateFormData = function (context) {
		var prefix = '#note-update-form ',
			id = AJS.$(context).attr("noteid"),
			priority =  AJS.$(context).attr("priorityid"),
			content = AJS.$(context).children(".content-detail").html(),
			user = AJS.$(context).children("#userid").html(),
			issue = AJS.$(context).children("#issueid").html();
		AJS.$('.pop-up-disable').remove();
		AJS.$(prefix + '#noteid').val(id);
		AJS.$(prefix + '#update-content').val(content);
		AJS.$(prefix + '#user-update-dlg').val(user);
		AJS.$(prefix + '#issue').val(issue);
		AJS.$(prefix + '#noteOperationErrorMessage').html('');
		update_chars_left(255, AJS.$(prefix + '#update-content'), AJS.$(prefix + '#update_remain'));
		if (priority === '1') {
			AJS.$(prefix + '#pri_1').attr('checked', true)
		} else if (priority === '2') {
			AJS.$(prefix + '#pri_2').attr('checked', true)
		}
	},

	showUpdateDialog = function () {
		if (!dlg_update) {
			AJS.$('#note-update-form').remove();
			initDialogUpdate();
		}
		resetUpdateFormData(this);		
		dlg_update.show();
	},
	
	onUpdateMemoByAjaxCompleted = function (data) {
		// if action success then reload page, else show the error message
		if (data.success === true) {
			dlg_update_memo.hide();
			// reload page if exists a input with name "reloadPageOnActionSuccess"
			if (AJS.$('input[name="reloadPageOnActionSuccess"]').length > 0) {
				reloadPage();
			} else {
				reloadLatestNote();
				reloadNoteIssueTabPanel();
			}
		} else {
			// show error message
			AJS.$('.pop-up-disable').remove();
			AJS.$('#memo-update-form #noteOperationErrorMessage').html(data.response.errorMessages[0]);
			AJS.$('#memo-update-form .error-row').show();
		}
	},
	
	// This function process the submit create form event 
	onSubmitUpdateMemoForm = function () {
		AJS.$('.dialog-components').append('<div class="aui-blanket pop-up-disable"></div>');
		var request = {
			url: AJS.contextPath() + "/UpdateMemo.jspa",
			data: {},
			dataType: "json",
			type: "GET",
			success: onUpdateMemoByAjaxCompleted
		};
		// set request data
		request.data['id'] = AJS.$('#memo-update-form input[name="id"]').val();
		request.data['issueKey'] = AJS.$('#memo-update-form input[name="issueKey"]').val();
		request.data['atl_token'] = AJS.$('#memo-update-form input[name="atl_token"]').val();
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	},
	
	initDialogUpdateMemo = function () { 
		dlg_update_memo = new AJS.Dialog({
			width : 600,
			height : 290,
			id : "memo-update-dialog",
			closeOnOutsideClick : false
		});
		dlg_update_memo.addHeader(AJS.I18n.getText("update.memo.name"));
		dlg_update_memo.addPanel(AJS.I18n.getText("panel.name"), AJS.renderTemplate("memo-dialog-update"));
		dlg_update_memo.addSubmit(AJS.I18n.getText("update.name"), onSubmitUpdateMemoForm);
		dlg_update_memo.addLink(AJS.I18n.getText("cancel.name"), function (dialog) {
			dialog.hide();
		});
	},
	showUpdateDialogMemo = function () {
		if (!dlg_update_memo) {
			AJS.$('#memo-update-form').remove();
			initDialogUpdateMemo();
		}
		var id = AJS.$(this).attr("memoid"),
			issue = AJS.$(this).children("#issueid").html(),
			content = AJS.$(this).children(".content-detail").html();
		AJS.$('.pop-up-disable').remove();
		AJS.$("#memo-update-form #memoid").val(id);
		AJS.$("#memo-update-form #update-content").html(content);
		AJS.$("#memo-update-form #issue").val(issue);
		AJS.$("#memo-update-form #noteOperationErrorMessage").html('');
		dlg_update_memo.show();
	};
	
	// Event
	AJS.$(".create-link").click(showCreateDialog);
	AJS.$(".note-editable").live('click', showUpdateDialog);
	AJS.$(".memo-editable").live('click', showUpdateDialogMemo);
	
	AJS.$('#note-update-form #update-content').live('keyup keydown',function(e) {
   		update_chars_left(255, AJS.$(this), AJS.$('#note-update-form #update_remain'));
 	});
	
});
